/**
 * 直线方程
 */
export default class MathLine {

    k: number;
    b: number;
    point1: cc.Vec2;
    point2: cc.Vec2;
    radian: number;

    static createByAngle(point: cc.Vec2, angle: number) {
        let point1 = point;
        let radian = angle * Math.PI / 180;
        let point2 = cc.v2(point1.x + Math.cos(radian), point1.y + Math.sin(radian));
        return new MathLine(point1.x, point1.y, point2.x, point2.y);
    }

    constructor(x1: number, y1: number, x2: number, y2: number) {
        this.point1 = cc.v2(x1, y1);
        this.point2 = cc.v2(x2, y2);
        this.k = (y2 - y1) / (x2 - x1);
        this.b = y1 - this.k * x1;
        this.radian = Math.atan2(y2 - y1, x2 - x1);
    }

    getX(y: number): number {
        return (y - this.b) / this.k;
    }

    getY(x: number): number {
        return this.k * x + this.b;
    }

}
